//
//  TXPRadioDetailViewController.m
//  StayAwhile
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TXPRadioDetailViewController.h"
#import "TXPRadioDetailView.h"
#import "TXPRadioDetailTableViewCell.h"
#import "RadioPlayerController.h"
@interface TXPRadioDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)NSMutableArray *radioInfoArray;
@property (nonatomic,strong)UITableView *myTableView;
@property (nonatomic,strong)NSMutableArray *alllistArray;
@end

@implementation TXPRadioDetailViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=NO;
    
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.alllistArray = [NSMutableArray array];
    self.radioInfoArray = [NSMutableArray array];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[TXPRadioDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.myTableView];
    [self postWithData];
}
- (void)postWithData
{
    if (self.model.url != 0) {
        NSString *str = [self.model.url substringFromIndex:12];
        NSLog(@"str%@,---%@",str,self.model.url);
        [TXPAFNetworkingBlock PostAFNURLWithString:@"http://api2.pianke.me/ting/radio_detail" Body:@{@"radioid":str,@"start":@"0",@"client":@"2",@"limit":@"10"} Cookie:@"PHPSESSID=8udrkkne3v9r8d2almesol6uh0" resultBlock:^(id result) {
            NSLog(@"%@",result);
            NSMutableDictionary *dic = [result objectForKey:@"data"];
            NSMutableArray *listArray = [dic objectForKey:@"list"];
            for (NSDictionary *dic in listArray) {
                TXPRadioModel *model = [TXPRadioModel modelWithDictionary:dic];
                [self.alllistArray addObject:model];
                
            }
            NSMutableDictionary *radioInfo = [dic objectForKey:@"radioInfo"];
            TXPRadioModel *model = [TXPRadioModel modelWithDictionary:radioInfo];
            [self.radioInfoArray addObject:model];
            NSLog(@"radioInfoArray=%@",self.radioInfoArray);

            [self.myTableView reloadData];
        } errorBlock:^(NSError *error) {
         
        }];
    }else{
        
        [TXPAFNetworkingBlock PostAFNURLWithString:@"http://api2.pianke.me/ting/radio_detail" Body:@{@"radioid":self.model.radioid,@"start":@"0",@"client":@"2",@"limit":@"10"} Cookie:@"PHPSESSID=8udrkkne3v9r8d2almesol6uh0" resultBlock:^(id result) {
            NSLog(@"-------%@",result);
            NSMutableDictionary *dic = [result objectForKey:@"data"];
            NSMutableArray *listArray = [dic objectForKey:@"list"];
            
            for (NSDictionary *dic in listArray) {
                
            TXPRadioModel *model = [TXPRadioModel modelWithDictionary:dic];
                [self.alllistArray addObject:model];
            }
            NSMutableDictionary *radioInfo = [dic objectForKey:@"radioInfo"];
            TXPRadioModel *model = [TXPRadioModel modelWithDictionary:radioInfo];
            [self.radioInfoArray addObject:model];
            NSLog(@"radioInfoArray=%@",self.radioInfoArray);
            [self.myTableView reloadData];
        } errorBlock:^(NSError *error) {
           
        }];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return HEIGHT * 0.45;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    TXPRadioDetailView *myView = [[TXPRadioDetailView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.45)];
    myView.backgroundColor = [UIColor whiteColor];
    myView.modelArray = self.radioInfoArray;
    return myView;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.alllistArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT * 0.13;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TXPRadioDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.alllistArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RadioPlayerController *player = [RadioPlayerController sharePlayerViewController];
    player.indexRow = indexPath.row;
   player.allMusicArray = self.alllistArray;
    TXPRadioModel *model = [self.alllistArray objectAtIndex:indexPath.row];
    player.tingid = model.tingid;
    NSLog(@"222222%@",player.tingid);
    [self presentViewController:player animated:YES completion:^{
        
        
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
